package com.example.shiXun.controller;

import com.example.shiXun.base.result.PageTableRequest;
import com.example.shiXun.base.result.Results;
import com.example.shiXun.domain.SysRole;
import com.example.shiXun.dto.RoleDto;
import com.example.shiXun.service.RoleService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;


/**
 * @author ：meet
 * @date ：Created in 2020/4/12 1:23
 */
@Controller
@RequestMapping("/role")
@Slf4j
public class RoleController {
    @Autowired
    private RoleService roleService;

    @GetMapping("/all")
    @ResponseBody
    public Results<SysRole> getAll(){
        log.info("RoleController.getAll()");
        return roleService.getAllRole();
    }

    @GetMapping("/list")
    @ResponseBody
    public Results<SysRole> list(PageTableRequest request){
        log.info("RoleController.list(): Param (request=" +request+ ")");
        request.countOffset();
        return roleService.getAllRoleByPage(request.getOffset(),request.getLimit());
    }

    @GetMapping("/findRoleByFuzzyName")
    @ResponseBody
    public Results<SysRole> findRoleByFuzzyName(String roleName,PageTableRequest request){
        log.info("RoleController.findRoleByFuzzyName(): Param (name ="+roleName+", request=" +request+ ")");
        request.countOffset();
        return roleService.getRoleByFuzzyName(roleName,request.getOffset(),request.getLimit());

    }

    @GetMapping("/add")
    public String addRole (Model model){
        model.addAttribute("sysRole",new SysRole());
        return "role/role-add";
    }

    @PostMapping("/add")
    @ResponseBody
    public Results saveRole(@RequestBody RoleDto roleDto){
        return roleService.save(roleDto);
    }

    @GetMapping("/edit")
    public String editRole (Model model, SysRole sysRole){
        model.addAttribute("sysRole",roleService.getRoleById(sysRole.getId()));
        return "/role/role-edit";
    }

    @PostMapping("/edit")
    @ResponseBody
    public Results updateRole(@RequestBody RoleDto roleDto){
        return roleService.update(roleDto);
    }

    @DeleteMapping("/delete")
    @ResponseBody
    public Results deleteRole (RoleDto roleDto){
        return roleService.delete(roleDto.getId());
    }



}
